Add git2-curl as a dependency
authorAlex Crichton <alex@alexcrichton.com>
Fri, 6 Feb 2015 07:28:18 +0000 (23:28 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 10 Feb 2015 05:26:13 +0000 (21:26 -0800)
Due to libgit2 not supporting HTTP proxies, the custom transport API of the
library must be used to reimplement the HTTP transport with proxy support. The
git2-curl crate implements this transport on top of the curl-rust crate. By
using libcurl we gain all sorts of proxy support for free.

This transport is not used by default, however, as it is not well battle tested
and the architecture is not currently ideal (download the entire repo into
memory on a clone). Only when an HTTP proxy is present is the new transport
used.

The other drawback of git2-curl is that it does not currently support
authentication. If a private git repository is cloned or authentication is
required then it will generate an error instead of correctly asking for
credentials.

Closes #636

Cargo.lock
Cargo.toml
src/bin/cargo.rs

index 32cbb704e91a0872d16f2aa6e2e55cbdcde7ad27..14dd8761b97de3314f610dd1d8bb8b32d02a75b0 100644 (file)
@@ -8,6 +8,7 @@ dependencies = [
  "env_logger 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "flate2 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "git2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
+ "git2-curl 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "glob 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
  "hamcrest 0.1.0 (git+https://github.com/carllerche/hamcrest-rust.git)",
  "log 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -100,6 +101,16 @@ dependencies = [
  "url 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "git2-curl"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "curl 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
+ "git2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
+ "url 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "glob"
 version = "0.1.9"
index e691eddd05a802231bd3ef37407b57750706aed8..2517dc56201e53d94cd52e0554ec5dc5578bba7b 100644 (file)
@@ -16,6 +16,7 @@ curl = "0.1"
 tar = "0.1"
 flate2 = "0.1"
 git2 = "0.1"
+git2-curl = "0.1"
 glob = "0.1"
 time = "0.1"
 log = "0.2"
index cdaa43adfe39d2d93284463654747b3518a21047..7bf0a1161ac13979412f7d165b2aefa77fc4b20b 100644 (file)
@@ -1,5 +1,6 @@
 #![feature(collections, core, io, path, env)]
 
+extern crate "git2-curl" as git2_curl;
 extern crate "rustc-serialize" as rustc_serialize;
 extern crate cargo;
 extern crate env_logger;
@@ -88,6 +89,8 @@ macro_rules! each_subcommand{ ($mac:ident) => ({
 fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
     config.shell().set_verbose(flags.flag_verbose);
 
+    init_git_transports(config);
+
     if flags.flag_list {
         println!("Installed Commands:");
         for command in list_commands().into_iter() {
@@ -247,3 +250,32 @@ fn list_command_directory() -> Vec<Path> {
     }
     dirs
 }
+
+fn init_git_transports(config: &Config) {
+    // Only use a custom transport if a proxy is configured, right now libgit2
+    // doesn't support proxies and we have to use a custom transport in this
+    // case. The custom transport, however, is not as well battle-tested.
+    match cargo::ops::http_proxy(config) {
+        Ok(Some(..)) => {}
+        _ => return
+    }
+
+    let handle = match cargo::ops::http_handle(config) {
+        Ok(handle) => handle,
+        Err(..) => return,
+    };
+
+    // The unsafety of the registration function derives from two aspects:
+    //
+    // 1. This call must be synchronized with all other registration calls as
+    //    well as construction of new transports.
+    // 2. The argument is leaked.
+    //
+    // We're clear on point (1) because this is only called at the start of this
+    // binary (we know what the state of the world looks like) and we're mostly
+    // clear on point (2) because we'd only free it after everything is done
+    // anyway
+    unsafe {
+        git2_curl::register(handle);
+    }
+}